<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class User_model extends CI_Model
{
	
	public function __construct()
	{
		parent::__construct();

	}
	
/*	public function get_my_tasks()
	{
		return $this->db->order_by('id','DESC')->get_where('tasks',array('user_id'=>$this->session->userdata('user_id')),5)->result();
	}*/
	
	public function get_my_tasks(){
		$this->db->select('tasks.project_id,tasks.*,projects.*,projects.id AS pro_id,tasks.id AS id' );
		$this->db->from('tasks');
		$this->db->where('tasks.user_id',$this->session->userdata('user_id'));
		$this->db->join('projects','projects.id = tasks.project_id');
		$this->db->order_by('tasks.id','DESC');
		$this->db->limit('5');
		//$this->db->join('projects','projects.id = tasks.project_id');
		return $this->db->get()->result();
	}

	
	public function get_fav_projects(){
		$this->db->select('favourites.*,projects.*');
		$this->db->from('favourites');
		$this->db->where('user_id',$this->session->userdata('user_id'));
		$this->db->join('projects','favourites.project_id = projects.id');
		return $this->db->get()->result();
	}
	
	public function get_fav_tasks(){
		$this->db->select('favourites.task_id,tasks.*,projects.*');
		$this->db->from('favourites');
		$this->db->where('favourites.user_id',$this->session->userdata('user_id'));
		$this->db->join('tasks','favourites.task_id = tasks.id');
		$this->db->join('projects','projects.id = tasks.project_id');
		return $this->db->get()->result();
	}
	public function  get_today_todo(){
		$where = array(
		 	'date_id' 		=> date('d'),
			'month_id'  	=> date('m'),	
			'year_id' 	 	=> date('y'),
			'user_id'  		=> $this->session->userdata('user_id'),
			'is_fav'   		=> 0,
			'priority'		=> "high"
		);
		return $this->db->get_where('to_do',$where)->result();
	}
	public function fav_todo($userid=NULL){
		
		$where = array(			
			'user_id'  		=> $this->session->userdata('user_id'),
			'is_fav'   		=> 1,
			'priority'		=> "high"
		);	
		
		$this->db->select('*');
		$this->db->from('to_do');
		$this->db->where($where);
		//$this->db->where('date_id <=',date('d'));
		//$this->db->where('month_id <=',date('m'));
		//$this->db->where('timestamp <=','CURDATE()');
		//$this->db->where('year_id',date('y'));
		return $this->db->get()->result();		
	}
			
	
	public function insert_drinks($insert)
	{
		$get_id = $this->db->get_where('drinks',array('attendance_timings_id'=>$insert['attendance_timings_id']))->row();		
		if(count($get_id)==0){
		$this->db->insert('drinks', $insert);
		}else{
		$this->db->update('drinks',array('drink'=>$insert['drink']),array('attendance_timings_id'=>$insert['attendance_timings_id']));
			}	
	}
	public function get_drinks()
	{	
		$this->db->select('*');
		$this->db->from('drinks');
		$this->db->join('attendance_timings','drinks.attendance_timings_id = attendance_timings.id');
		$this->db->join('users','attendance_timings.user_id = users.id','left');
		$this->db->where('attendance_timings.date_id',date('d'));
		$this->db->where('attendance_timings.month_id',date('m'));
		$this->db->where('attendance_timings.year_id',date('y'));
		return $this->db->get()->result();
	}
	public function count_tea()
	{
		$this->db->select('drinks.*','attendance_timings.*');
		$this->db->from('drinks'); 
		$this->db->join('attendance_timings','drinks.attendance_timings_id = attendance_timings.id');
		$this->db->where('drinks.drink','tea');
		$this->db->where('attendance_timings.date_id',date('d'));
		$this->db->where('attendance_timings.month_id',date('m'));
		$this->db->where('attendance_timings.year_id',date('y'));
		return $this->db->count_all_results();
	}
	public function count_coffee()
	{
		$this->db->select('drinks.*','attendance_timings.*');
		$this->db->from('drinks'); 
		$this->db->join('attendance_timings','drinks.attendance_timings_id = attendance_timings.id');
		$this->db->where('drinks.drink','coffee');
		$this->db->where('attendance_timings.date_id',date('d'));
		$this->db->where('attendance_timings.month_id',date('m'));
		$this->db->where('attendance_timings.year_id',date('y'));
		return $this->db->count_all_results();
	}
	public function count_soup()
	{
		$this->db->select('drinks.*','attendance_timings.*');
		$this->db->from('drinks'); 
		$this->db->join('attendance_timings','drinks.attendance_timings_id = attendance_timings.id');
		$this->db->where('drinks.drink','soup');
		$this->db->where('attendance_timings.date_id',date('d'));
		$this->db->where('attendance_timings.month_id',date('m'));
		$this->db->where('attendance_timings.year_id',date('y'));
		return $this->db->count_all_results();
	}

	
	public function more_todos(){		
		$where = array(
		 	'date_id' 		=> date('d'),
			'month_id'  	=> date('m'),	
			'year_id' 	 	=> date('y'),
			'user_id'  		=> $this->session->userdata('user_id'),
			'priority !='		=> "high"
		);
		
		return $this->db->get_where('to_do',$where)->result();
		
	}
	
	public function todos_not_dated(){		
		$getit = array(
			
			'user_id'  		=> $this->session->userdata('user_id'),
			'priority !='	=> "high",
			'is_fav ='		=> 1,
			'date_id !=' 	=> date('d'),
			'month_id'  	=> date('m'),	
			'year_id' 	 	=> date('y'),
		);
		
		return $this->db->get_where('to_do',$getit)->result();
	}
	
	public function get_active_user()
	{
		return $this->db->get_where('users',array('online_status'=>1))->result();		
	}
	
		
	public function previous_todo($user_id=null){		
		$month=date('m');
		$user_id = isset($user_id) ? $user_id : $this->session->userdata('user_id');
		$this->db->select('*');
		$this->db->from('to_do');
		$this->db->where('user_id',$user_id);
		$this->db->where('is_fav',1);
		$this->db->where('month_id !=',$month);
		$this->db->order_by('date_id ','ASC');
		return $this->db->get()->result();
		}
	
	
}
